package cn.fl.project.controller;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.logger.eum.LogOperEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DecimalUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.preloan.enums.LogBusinessTypeEnum;
import cn.fl.project.condition.PrjQuatPrpsRebQueryCondition;
import cn.fl.project.service.IPrjQuatPrpsCfgMService;
import cn.fl.project.service.IPrjQuatPrpsRebService;
import cn.fl.project.vo.PrjQuatPrpsCfgMVO;
import cn.fl.project.vo.PrjQuatPrpsRebVO;
import cn.fl.system.constant.SysLogConstant;
import com.alibaba.fastjson.JSON;
import com.lionbridge.log.ext.ExtSlf4jLogger;
import com.lionbridge.log.ext.ExtSlf4jLoggerBuilder;
import com.lionbridge.log.ext.LogTypeEnum;
import fl.constant.facade.framework.constant.BusinessIdConstant;
import fl.spl.facade.framework.constant.SplSystemConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 项目报价方案融资额返利配置表 控制层
 */
@Slf4j
@Controller
@RequestMapping("/project/prjQuatPrpsReb")
public class PrjQuatPrpsRebController extends BaseController {
	private static ExtSlf4jLogger logger = ExtSlf4jLoggerBuilder.create()
			.setLogger(log).setBusinessType(LogBusinessTypeEnum.QUOTATION_SCHEME.getCode())
			.setSubBusinessType(LogBusinessTypeEnum.QUOTATION_SCHEME.getCode())
			.setLogType(LogTypeEnum.BUSINESS).build().setKeywords("PrjQuatPrpsReb");
	/**
	 * PrjQuatPrpsReb 服务层BEAN
	 */
	@Autowired
	private IPrjQuatPrpsRebService prjQuatPrpsRebService;
	
	@Autowired
	private IPrjQuatPrpsCfgMService prjQuatPrpsCfgMService;

	/**
	 * 到列表页面 - 融资返利比例
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("project/prjQuatPrpsReb/prjQuatPrpsRebAddList");

		String prpsCfgId = request.getParameter("prpsCfgId");
		view.addObject("prpsCfgId", prpsCfgId);
		String gpsRebBscPct = request.getParameter("gpsRebBscPct");
		String gpsRebBscPc = request.getParameter("gpsRebBscPc");
		String lsRebBscPct = request.getParameter("lsRebBscPct");
		view.addObject("gpsRebBscPct", StringUtils.isNotEmpty(gpsRebBscPct)?gpsRebBscPct:"");
		view.addObject("gpsRebBscPc", StringUtils.isNotEmpty(gpsRebBscPc)?DecimalUtils.convertFenToYuan(Long.valueOf(gpsRebBscPc)):"");
		view.addObject("lsRebBscPct", StringUtils.isNotEmpty(lsRebBscPct)?lsRebBscPct:"");
		
		//是否
		List<KeyValueVO> yesOrNoCdList = this.prjQuatPrpsCfgMService.searchYesOrNoCdList();
		view.addObject("yesOrNoCdList", yesOrNoCdList);
		view.addObject("yesOrNoCdListJSON", JSON.toJSONString(yesOrNoCdList));
		
		String isDetail = request.getParameter("isDetail");
		view.addObject("isDetail", isDetail);
		return view;
	}
	
	/**
	 * 到列表页面 - 手续费返利比例
	 */
	@RequestMapping(value = "/toListPage2.do", method = { RequestMethod.GET})
	public ModelAndView toListPage2(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("project/prjQuatPrpsReb/prjQuatPrpsRebAddList2");

		String prpsCfgId = request.getParameter("prpsCfgId");
		view.addObject("prpsCfgId", prpsCfgId);
		
		//是否
		List<KeyValueVO> yesOrNoCdList = this.prjQuatPrpsCfgMService.searchYesOrNoCdList();
		view.addObject("yesOrNoCdList", yesOrNoCdList);
		view.addObject("yesOrNoCdListJSON", JSON.toJSONString(yesOrNoCdList));
		
		String isDetail = request.getParameter("isDetail");
		view.addObject("isDetail", isDetail);
		return view;
	}

	/**
	 * 到修改页面
	 */
	@RequestMapping(value = "/toModifyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		ModelAndView view = new ModelAndView("project/prjQuatPrpsReb/modify");
		view.addObject("id", id);
		return view;
	}

	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String prpsCfgId = request.getParameter("prpsCfgId");
		String pctTypCd = request.getParameter("pctTypCd");
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		PrjQuatPrpsRebQueryCondition qc = new PrjQuatPrpsRebQueryCondition();
		if (StringUtils.isNotEmpty(prpsCfgId)) {
			qc.setPrpsCfgId(Long.valueOf(prpsCfgId));
		}else{
			return null;
		}
		if(StringUtils.isNotEmpty(pctTypCd)){
			qc.setPctTypCd(pctTypCd);
		}
		page.setCondition(qc);

		Pager pager = this.prjQuatPrpsRebService.searchListPage(page);
		List<PrjQuatPrpsRebVO> list = (List<PrjQuatPrpsRebVO>) pager.getResultList();
		ListPageVO<PrjQuatPrpsRebVO> listPage = new ListPageVO<PrjQuatPrpsRebVO>(list, pager.getTotalCount());
		return listPage;
	}
	
	/**
	 * 新增
	 */
	@RequestMapping(value = "/batchAdd.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object batchAdd(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		PrjQuatPrpsCfgMVO entity = new PrjQuatPrpsCfgMVO();
		try {
			entity = this.getRequestParamForAdd(request);
			boolean success = this.prjQuatPrpsRebService.batchAdd(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_OPERATE_FAIL);
				return result;
			}
			logger.info("项目报价方案融资额返利配置表，新增数据entity:{}", JSON.toJSONString(entity));
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			logger.error("项目报价方案融资额返利配置表，新增数据,业务异常entity:{},msg:{}", JSON.toJSONString(entity),ex.getMessage());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			logger.error("项目报价方案融资额返利配置表，新增数据,系统异常entity：{},msg:{}", JSON.toJSONString(entity),ex.getMessage());
		}

		return result;
	}

	/**
	 * 删除数据
	 */
	@RequestMapping(value = "/removeById.do", method = {RequestMethod.POST })
	@ResponseBody
	public DataResultVO removeById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			return result;
		}
		try {
			this.prjQuatPrpsRebService.removeById(Long.valueOf(id));
			logger.info("项目报价方案融资额返利配置表，删除数据id：{}", id);
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			logger.error("项目报价方案融资额返利配置表，删除数据失败id:{}", id);
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			logger.error("项目报价方案融资额返利配置表，删除数据失败,系统异常msg:{}",ex.getMessage());
		}

		return result;
	}

	/**
	 * 得到一条记录
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			logger.info("项目报价方案融资额返利配置表，获取详情，id为空");
			return result;
		}
		
		try {
			PrjQuatPrpsRebVO vo = this.prjQuatPrpsRebService.selectById(Long.valueOf(id));
			if(null == vo){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
				logger.info("项目报价方案融资额返利配置表，获取详情，结果为空");
			}
			result.setData(vo);
			logger.info("项目报价方案融资额返利配置表，获取详情，返回结果result:{}", JSON.toJSONString(result));
		}catch(BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			logger.error("项目报价方案融资额返利配置表，获取详情，业务异常,msg:{}",ex.getMessage());
		}catch(Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			logger.error("项目报价方案融资额返利配置表，获取详情，系统异常msg:{}",ex.getMessage());
		}
		return result;
	}

	/**
	 * 新增
	 */
	@RequestMapping(value = "/add.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object add(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
		PrjQuatPrpsRebVO entity = null;
		try {
			entity = new PrjQuatPrpsRebVO();
			Long id = this.prjQuatPrpsRebService.add(entity);
			result.setData(id);
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
		}

		return result;
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/modify.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modify(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			return result;
		}
		PrjQuatPrpsRebVO entity = null;
		try {
			entity = new PrjQuatPrpsRebVO();
			entity.setId(Long.valueOf(id));
			//entity.setName(name);
			boolean success = this.prjQuatPrpsRebService.modify(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			}
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
		}
		return result;
	}
	
	//************************************************私有方法*********************************************
	/**
	 * 获取请求参数
	 * @param request
	 * @return
	 * @throws BaseException
	 */
	private PrjQuatPrpsCfgMVO getRequestParamForPrjQuatPrpsCfgAi(HttpServletRequest request) throws BaseException {
		String gpsRebBscPct = request.getParameter("gpsRebBscPct");//GPS返利基准比例(%)
		String gpsRebBscPc = request.getParameter("gpsRebBscPc");//GPS返利基准价格
		String lsRebBscPct = request.getParameter("lsRebBscPct");//融资额返利基准比例(%)
		
		String rebInfo = request.getParameter("rebInfo");
		//解析rebInfo
		List<PrjQuatPrpsRebVO> prjQuatPrpsRebVOList = new ArrayList<PrjQuatPrpsRebVO>();
		if (StringUtils.isNotEmpty(rebInfo)) {
			prjQuatPrpsRebVOList = JSON.parseArray(rebInfo, PrjQuatPrpsRebVO.class);
		}
		
		
		PrjQuatPrpsCfgMVO prjQuatPrpsCfgMVO = new PrjQuatPrpsCfgMVO();
		if(StringUtils.isNotEmpty(gpsRebBscPct)){
			prjQuatPrpsCfgMVO.setGpsRebBscPct(Double.valueOf(gpsRebBscPct));
		}
		if(StringUtils.isNotEmpty(gpsRebBscPc)){
			prjQuatPrpsCfgMVO.setGpsRebBscPc(DecimalUtils.convertYuanToFen(Double.valueOf(gpsRebBscPc)));
		}
		if(StringUtils.isNotEmpty(lsRebBscPct)){
			prjQuatPrpsCfgMVO.setLsRebBscPct(Double.valueOf(lsRebBscPct));
		}
		
		prjQuatPrpsCfgMVO.setPrjQuatPrpsRebVOList(prjQuatPrpsRebVOList);
		
		return prjQuatPrpsCfgMVO;
	}
	
	/**
	 * 获取报价方案请求参数(新增)
	 * @param request 请求
	 * @return 
	 */
	private PrjQuatPrpsCfgMVO getRequestParamForAdd(HttpServletRequest request)throws BaseException{
		String prpsCfgId = request.getParameter("prpsCfgId");
		if (StringUtils.isBlank(prpsCfgId)) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_604, "请先维护报价方案基本信息!");
		}
		PrjQuatPrpsCfgMVO prjQuatPrpsCfgMVO = this.getRequestParamForPrjQuatPrpsCfgAi(request);
		prjQuatPrpsCfgMVO.setId(Long.valueOf(prpsCfgId));
		prjQuatPrpsCfgMVO.setMdfTm(new Date());
		prjQuatPrpsCfgMVO.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
		List<PrjQuatPrpsRebVO> prjQuatPrpsRebVOList = prjQuatPrpsCfgMVO.getPrjQuatPrpsRebVOList();
		if(CollectionUtils.isNotEmpty(prjQuatPrpsRebVOList)){
			for (PrjQuatPrpsRebVO prjQuatPrpsRebVO : prjQuatPrpsRebVOList) {
				if(null!=prjQuatPrpsRebVO && null!=prjQuatPrpsRebVO.getId()){
					prjQuatPrpsRebVO.setPrpsCfgId(Long.valueOf(prpsCfgId));
					prjQuatPrpsRebVO.setMdfTm(new Date());
					prjQuatPrpsRebVO.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
				}else if(null!=prjQuatPrpsRebVO){
					prjQuatPrpsRebVO.setPrpsCfgId(Long.valueOf(prpsCfgId));
					prjQuatPrpsRebVO.setCrtTm(new Date());
					prjQuatPrpsRebVO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
				}
			}
		}
		return prjQuatPrpsCfgMVO;
	}
	
}

